package com.loginlib.login.dialog;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.TextView;

import com.liulishuo.filedownloader.BaseDownloadTask;
import com.liulishuo.filedownloader.FileDownloadLargeFileListener;
import com.loginlib.BuildConfig;
import com.loginlib.R;
import com.loginlib.di.component.DaggerLoginActivityComponent;
import com.loginlib.di.component.LoginActivityComponent;
import com.loginlib.di.module.LoginModule;
import com.loginlib.login.LoginActivity;
import com.loginlib.panel.InitPanel;
import com.loginlib.presenters.InitDataPresenter;
import com.wanhe.base.Const;
import com.wanhe.base.util.FileDownLoadUtil;
import com.wanhe.base.util.Logger;
import com.wanhe.base.util.ObjectUtil;
import com.wanhe.base.util.SPUtils;
import com.wanhe.base.util.SystemUtil;
import com.wanhe.base.util.ViewUtil;

import java.io.File;

import javax.inject.Inject;

/**
 * 加载初始化数据的对话框
 *
 * @author 陈德基
 *         create at 2017/4/21 12:21
 */
public class InitDataDialog extends Dialog implements InitPanel, DialogInterface.OnDismissListener {
    private TextView loadingDetail;
    @Inject
    InitDataPresenter presenter;
    LoginActivityComponent loginActivityComponent;
    private int readyImageCount;

    Context context;


    public InitDataDialog(Context context) {
        super(context, R.style.dialog);
        this.context = context;
    }

    public InitDataDialog(Context context, int themeResId) {
        super(context, themeResId);
    }

    public void initInject() {
        loginActivityComponent = DaggerLoginActivityComponent.builder().loginModule(new LoginModule(getContext())).build();
        loginActivityComponent.inject(this);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.initial_data_layout);
        setCanceledOnTouchOutside(false);
        setCancelable(false);
        initInject();
        // 设置对话框横向充满，垂直自适应并居中
        Window mWindow = this.getWindow();
        int[] widthAndHeight = SystemUtil.getWindowWidthAndHeight(getContext());
        float width = widthAndHeight[0] * 0.3f;
        float height = widthAndHeight[1] * 0.3f;
        mWindow.setLayout((int) width, (int) height);
        mWindow.setGravity(Gravity.CENTER_VERTICAL);

        loadingDetail = (TextView) findViewById(R.id.tv_loading_detail);

//        if (!BuildConfig.DEBUG){
//            loadingDetail.setVisibility(View.GONE);
//        }
        presenter.setInitPanel(this);
        presenter.getAllDish();
        initAppCacheData();

        setOnDismissListener(this);

    }

    /**
     * 开始加载应用缓存数据
     *
     * @author 陈德基
     * create at 2017/4/21 14:02
     */
    private void initAppCacheData() {
    }


    //============================================初始化设计器回调======================================================


    @Override
    public void onInitDesignDataFail() {
        loadingDetail.setTextColor(Color.RED);
        loadingDetail.setText("应用初始化菜单界面数据失败，请检查网络配置！");
        setCancelable(true);
    }

    @Override
    public void onDesignDataErrUseCacheData() {
        loadingDetail.setTextColor(Color.YELLOW);
        loadingDetail.setText("同步最新菜单界面数据失败，使用缓存数据");
        presenter.getSellOutData();
    }

    @Override
    public void onLoadingDesignBGImageProcess(final int pageCount, final String absolutePath) {
        Log.d("initData", "pageCount=" + pageCount);
        loadingDetail.post(new Runnable() {
            @Override
            public void run() {
                loadingDetail.setTextColor(Color.GREEN);
                if (++readyImageCount != pageCount) {
                    loadingDetail.setText("正在加载菜单数据......" + absolutePath);
                } else {
                    loadingDetail.setText("菜单背景图加载完毕");
                    //加载估清数据
                    presenter.getSellOutData();
                }
            }
        });
    }

    @Override
    public void onDishDataErrUseCacheData() {
        loadingDetail.setTextColor(Color.YELLOW);
        loadingDetail.setText("同步最新菜品数据失败，使用缓存数据");
        presenter.getDesignData();
    }

    @Override
    public void onInitDishDataFail() {
        loadingDetail.setTextColor(Color.RED);
        loadingDetail.setText("应用初始化菜品数据失败，请检查网络配置！");
        setCancelable(true);
    }

    @Override
    public void onInitDishDataSuccess() {
        Log.d("initData", "SPUtils.getAllDish()=" + SPUtils.getAllDish());
        loadingDetail.post(new Runnable() {
            @Override
            public void run() {
                loadingDetail.setTextColor(Color.GREEN);
                loadingDetail.setText("同步最新菜品数据成功！");
            }
        });
        presenter.getDesignData();
    }

    @Override
    public void onSallOutDataErrUseCache() {
        loadingDetail.setTextColor(Color.YELLOW);
        loadingDetail.setText("同步最新估清数据失败，使用缓存数据");

        loadingDetail.postDelayed(new Runnable() {
            @Override
            public void run() {
                dismiss();
                /**
                 * 需要添加演示流程的判断
                 * @author 陈德基
                 * create at 2017/6/2 16:33
                 */
                boolean isDemonstration = SPUtils.getIsDemoSchema();

                if (isDemonstration) {
//                    Intent intent = new Intent(getContext(), ChuiyuMainActivity.class);
//                    getContext().startActivity(intent);
                    if (SPUtils.getIsDemoSchema()) {
                        Intent intent = new Intent();
                        intent.setAction("com.wanhe.meal");
                        getContext().startActivity(intent);
                    }
                } else {
                    Intent intent = new Intent(getContext(), LoginActivity.class);
                    getContext().startActivity(intent);
                }
            }
        }, 2000);

    }

    @Override
    public void onSallOutDataInitFail() {
        loadingDetail.setTextColor(Color.RED);
        loadingDetail.setText("应用初始化菜品估清数据失败，请检查网络配置！");
        setCancelable(true);
    }

    @Override
    public void onSallOutDataInitSuccess() {
        loadingDetail.setTextColor(Color.GREEN);
        loadingDetail.setText("同步最新菜品估清数据成功！2秒后进入应用");

        loadingDetail.postDelayed(new Runnable() {
            @Override
            public void run() {
                dismiss();
                /**
                 * 需要添加演示流程的判断
                 * @author 陈德基
                 * create at 2017/6/2 16:33
                 */
                boolean isDemonstration = SPUtils.getIsDemoSchema();

                if (isDemonstration) {
//                    Intent intent = new Intent(getContext(), ChuiyuMainActivity.class);
//                    getContext().startActivity(intent);
                    if (SPUtils.getIsDemoSchema()) {
                        Intent intent = new Intent();
                        intent.setAction("com.wanhe.meal");
                        getContext().startActivity(intent);
                    }
                } else {
                    Intent intent = new Intent(getContext(), LoginActivity.class);
                    getContext().startActivity(intent);
                }
            }
        }, 2000);

    }




    //============================================普通回调======================================================

    @Override
    public void onLoading() {

    }

    @Override
    public void onLoaded() {

    }

    @Override
    public void onLoadFail(String msg) {

    }

    @Override
    public void onDismiss(DialogInterface dialog) {
        ViewUtil.dismissLoadingDialog();
        if (ObjectUtil.checkObject(presenter)) presenter.onDestroy();
    }


}
